package com.lcu.student.service.impl;

import com.lcu.Result.Result;
import com.lcu.student.entity.User;
import com.lcu.student.mapper.UserMapper;
import com.lcu.student.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public Result addUser(User user) {
        int adduser = userMapper.adduser(user.getUserName(), user.getUserPassword());
        if (adduser==1){
            return Result.success();
        }
        return Result.error("注册失败，请稍后再试！");
    }

    /**
     * 判断密码是否正确
     * @param userName
     * @param password
     * @return
     */
    @Override
    public Result get(String userName, String password) {
        User user = userMapper.get(userName);
        if (user==null){
            return Result.error("404","用户名不存在！");
        }else if (!user.getUserPassword().equals(password)){
            return Result.error("400","密码输入错误！");
        }else {
            return Result.success("登录成功！",user.getUserName());
        }
    }

    /**
     * 根据用户名查出用户信息
     * @param user
     * @return
     */
    @Override
    public Result selectUser(User user) {
        User user1 = userMapper.selectUser(user.getUserName());
        return Result.success(user1);
    }

    /**
     * 根据用户id修改密码
     * @param id
     * @param password
     * @return
     */
    @Override
    public Result updateById(int id, String password) {
        userMapper.updateById(id,password);
        return Result.success();
    }


}
